package com.cf.charging.service.handle;
import java.nio.charset.Charset;
import java.util.Map;

import com.cf.charging.service.impl.CfChargingStationBusinessServiceImpl;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.CharsetUtil;

public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

    /**
     * 向服务端发送数据
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("客户端与服务端通道-开启：" + ctx.channel().localAddress() + "channelActive");

//        String sendInfo = "Hello 这里是客户端  你好啊！";
//        System.out.println("客户端准备发送的数据包：" + sendInfo);
//        ctx.writeAndFlush(Unpooled.copiedBuffer(sendInfo, CharsetUtil.UTF_8)); // 必须有flush

    }

    /**
     * channelInactive
     *
     * channel 通道 Inactive 不活跃的
     *
     * 当客户端主动断开服务端的链接后，这个通道就是不活跃的。也就是说客户端与服务端的关闭了通信通道并且不可以传输数据
     *
     */
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("客户端与服务端通道-关闭：" + ctx.channel().localAddress() + "channelInactive");
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        System.out.println("读取客户端通道信息..");
        ByteBuf buf = msg.readBytes(msg.readableBytes());
        System.out.println(
                "客户端接收到的服务端信息:" + ByteBufUtil.hexDump(buf) + "; 数据包为:" + buf.toString(Charset.forName("utf-8")));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        Map<String, Channel> channelMap = CfChargingStationBusinessServiceImpl.channelMap;
        Map<String, NioEventLoopGroup> groupMap = CfChargingStationBusinessServiceImpl.groupMap;
        for(Map.Entry<String, Channel> entry : channelMap.entrySet()){
            if(entry.getValue().id().equals(ctx.channel().id())){
                //移除和关闭channel
                channelMap.remove(entry.getKey());
                if(entry.getValue().isActive()){
                    entry.getValue().close();
                }
                //移除和关闭相关线程池
                groupMap.remove(entry.getKey());
                groupMap.get(entry.getKey()).shutdownGracefully().sync();
                break;
            }
        }
        ctx.close();
        System.out.println("异常退出:" + cause.getMessage());
    }

}
